from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serializers.spu_serializers import SPUSerializer, BrandSerializer, GoodsCategorySerializer
from meiduo_admin.utils import MyPagination


class SPUGoodsView(ModelViewSet):
    """ SPU表数据 增删改查 """

    queryset = SPU.objects.all().order_by("id")
    serializer_class = SPUSerializer
    # 使用自定义的分页器
    pagination_class = MyPagination


class SPUBrandView(ListAPIView):
    """ 获取品牌信息 """

    queryset = Brand.objects.all()
    serializer_class = BrandSerializer
    # 不使用分页器
    pagination_class = None


class ChannelCategoryView(ListAPIView):
    """ 获取商品类别 一级分类、二级分类、三级分类 """

    queryset = GoodsCategory.objects.all()
    serializer_class = GoodsCategorySerializer
    pagination_class = None

    def get_queryset(self):
        parent_id = self.kwargs.get("pk")
        if parent_id:
            return self.queryset.filter(parent_id=parent_id)
        else:
            # 如果 parent_id 为空
            return self.queryset.filter(parent_id__isnull=True)

